<template>
    <div class="header">
        <h1>城市空气质量实时发布平台</h1>
        <div class="refresh">
            <button @click="fetchData">刷新</button>
            <span>{{ currentTime }}</span>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const currentTime = ref('')
const airData = ref(null) // 定义 airData 变量

const fetchData = async () => {
    try {
        console.log('开始获取数据...')
        const response = await fetch("http://127.0.0.1:8765/get", {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                area: "惠州市"
            })
        })
        console.log('请求响应:', response)
        
        const result = await response.json()
        console.log('获取到的数据:', result)
        
        if (result.status === 0 && result.data.rows.length > 0) {
            airData.value = result.data.rows[0]
            updateCurrentTime()
        } else {
            console.error('数据格式不正确:', result)
        }
    } catch (error) {
        console.error("获取空气质量数据失败", error)
    }
}

const updateCurrentTime = () => {
    const now = new Date()
    currentTime.value = `${now.getMonth() + 1}月${now.getDate()}日${now.getHours()}:${String(now.getMinutes()).padStart(2, '0')}`
}

onMounted(() => {
    fetchData()
})
</script>

<style scoped>
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #f5f5f5;
    padding: 20px;
    border-bottom: 1px solid #ddd;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    margin: 5px;
    height: 75px; /* 设置高度为100px */
}

.header h1 {
    margin: 0;
    font-size: 24px;
    color: #333;
    font-family: 'Arial', sans-serif;
}

.refresh {
    display: flex;
    align-items: center;
}

.refresh button {
    padding: 8px 16px;
    font-size: 16px;
    cursor: pointer;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    transition: background-color 0.3s ease;
}

.refresh button:hover {
    background-color: #0056b3;
}

.refresh span {
    margin-left: 10px;
    font-size: 16px;
    color: #666;
}
</style>